﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports Exp.DAL
Imports Exp.Model


Public Class Owner_Service
    Private _objOwner As New REM_Owner


    Public Sub New(objOwner As REM_Owner)
        _objOwner = objOwner
    End Sub

    Public Sub New()

    End Sub

    Public Function GetREM_Owner() As List(Of REM_Owner)
        Dim ctx As New EXPEntities
        Dim list = ctx.REM_Owner.ToList
        Return list
    End Function

    Public Function GetAll() As List(Of EXP_Select_REM_Owner_ByUserID_Result)
        Dim ctx As New EXPEntities
        Dim list = ctx.EXP_Select_REM_Owner_ByUserID(0).ToList
        Return list
    End Function

    Public Function GetByUserIDAndUnitID(ByVal iUserID As Integer, ByVal iUnitID As Integer) As List(Of REM_Owner)
        Dim ctx As New EXPEntities
        Dim lstObj As New List(Of REM_Owner)

        If iUserID <> 0 Then
            lstObj = ctx.REM_Owner.Where(Function(o) o.User_ID = iUserID And o.Unit_ID = iUnitID).ToList()
        Else
            lstObj = ctx.REM_Owner.Where(Function(o) o.Unit_ID = iUnitID).OrderByDescending(Function(o) o.CreateDate).ToList
        End If

        Return lstObj
    End Function
    Public Function GetByUser_ID(ByVal User_ID As Integer) As List(Of REM_Owner)
        Dim ctx As New EXPEntities
        Dim obj = ctx.REM_Owner.Where(Function(o) o.User_ID = User_ID).ToList
        Return obj
    End Function


    Public Sub Add(objREM_Owner As REM_Owner)
        Dim ctx As New EXPEntities
        ctx.REM_Owner.AddObject(objREM_Owner)
        ctx.SaveChanges()
    End Sub

    Public Sub Update(objREM_Owner As REM_Owner)
        Try
            Dim ctx As New EXPEntities
            Dim obj = ctx.REM_Owner.Where(Function(o) o.User_ID = objREM_Owner.User_ID And o.Unit_ID = objREM_Owner.Unit_ID).FirstOrDefault()
            obj.Quota = objREM_Owner.Quota
            obj.UpdateUser = objREM_Owner.UpdateUser
            obj.UpdateDate = objREM_Owner.UpdateDate

            ctx.SaveChanges()
        Catch ex As Exception

        End Try
    End Sub

    Public Function GetOwner_ByUserID(ByVal user_id As Integer) As List(Of EXP_Select_REM_Owner_ByUserID_Result)
        Dim ctx As New EXPEntities
        Dim obj = ctx.EXP_Select_REM_Owner_ByUserID(user_id).ToList

        Return obj

    End Function

    Public Sub Delete(ByVal User_ID As Integer, ByVal Unit_ID As Integer)
        Dim ctx As New EXPEntities
        Dim obj = ctx.REM_Owner.Where(Function(o) o.User_ID = User_ID And o.Unit_ID = Unit_ID).FirstOrDefault()
        ctx.REM_Owner.DeleteObject(obj)
        ctx.SaveChanges()
    End Sub


    Public Sub UpdateQaota(ByVal unit_ID As Integer, ByVal user_ID As Integer, ByVal Quota As Integer, ByVal updateuser As Integer)
        Dim ctx As New EXPEntities
        Dim obj = ctx.REM_Owner.Where(Function(o) o.Unit_ID = unit_ID And o.User_ID = user_ID).FirstOrDefault()
        obj.Quota = Quota
        obj.UpdateUser = updateuser
        obj.UpdateDate = Now
        ctx.SaveChanges()
    End Sub

    Public Sub UpdateShareDate(ByVal unit_id As Integer, ByVal stDate As Date, ByVal endDate As Date, ByVal user_id As Integer)
        Dim ctx As New EXPEntities
        Dim obj = ctx.REM_Owner.Where(Function(o) o.Unit_ID = unit_id).OrderByDescending(Function(o) o.CreateDate).FirstOrDefault
        obj.ShareStartDate = stDate
        obj.ShareEndDate = endDate
        Dim q As Integer = DateDiff(DateInterval.Day, stDate, endDate)
        obj.Quota = q
        obj.UpdateDate = Now
        obj.UpdateUser = user_id
        ctx.SaveChanges()
    End Sub
End Class


